package com.victor.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.victor.common.core.domain.R;
import com.victor.common.core.utils.EncryptUtils;
import com.victor.common.entity.system.SysThirdKey;
import com.victor.common.request.PageRequest;
import com.victor.system.service.SysThirdKeyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  controller
 * </p>
 *
 * @author victor
 * @since 2023年06月14日
 */
@Api(tags = "REST - 第三方密钥管理")
@RestController
@RequestMapping("/sysThirdKey")
public class SysThirdKeyController {
    @Autowired
    SysThirdKeyService service;
                        
    @PostMapping("/create")
    @ApiOperation(value = "新增")
    public R<?> createOne(@RequestParam("company") String company){

        boolean flag=service.saveCompany(company);
        return R.ok(flag,flag?"创建成功":"创建失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public R<?> delete(@PathVariable("id")  Integer  id){
        boolean flag = service.removeById(id);
        return R.ok(flag, flag ? "删除成功" : "删除失败");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public R<?> update(@RequestBody SysThirdKey body){

        boolean flag=service.updateKeyInfoById(body);
        return R.ok(flag,flag?"修改成功":"修改失败");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "单条查询")
    public R<?> getOne(@PathVariable("id")  Integer  id){

        return R.ok(service.getById(id));
    }


    @PostMapping("/findList")
    @ApiOperation(value = "列表查询")
    public R<List<SysThirdKey>> findList(@RequestBody SysThirdKey body){

        return R.ok(service.list(new LambdaQueryWrapper<SysThirdKey>(body)));
    }

    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询")
    public R findPage(@RequestBody SysThirdKey body, PageRequest pageRequest){
        Page<SysThirdKey> page=new Page<>(pageRequest.getCurrent(),pageRequest.getSize());
        return R.ok(service.page(page,new LambdaQueryWrapper<SysThirdKey>(body).orderByDesc(SysThirdKey::getId)));
    }

    @PostMapping("/updateThirdToken")
    @ApiOperation(value = "更新第三方(医丞)的token")
    public R<?> updateThirdToken(@RequestBody SysThirdKey body){
        return R.ok(service.updateThirdToken(body));
    }

    @GetMapping("/getYCThirdMsg")
    @ApiOperation(value = "获取第三方(医丞)信息")
    public R<SysThirdKey> getYCThirdMsg(){
        return R.ok(service.getOne(new LambdaQueryWrapper<SysThirdKey>().eq(SysThirdKey::getCompany,"医丞")));
    }
}